<template>
  <div class="login">
    <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
      <el-form-item label="用户名">
        <el-input placeholder="请输入用户名" v-model="username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input placeholder="请输入密码" v-model="password" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <router-link to="/">
          <el-button type="primary" @click="handleLogin" class="btn">
            登 陆
          </el-button>
        </router-link>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { reactive, toRefs, ref } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
export default {
  setup() {
    const store = useStore();
    const router = useRouter();
    const labelPosition = ref("right");
    const data = reactive({
      formLabelAlign: {
        username: "admin",
        password: "942642",
      },
    });
    function handleLogin() {
      store
        .dispatch("login", data.formLabelAlign)
        .then(() => {
          router.push({ path: "/" });
          // this.loading = false;
        })
        .catch(() => {
          // this.loading = false;
        });
    }
    return {
      handleLogin,
      labelPosition,
      ...toRefs(data.formLabelAlign),
    };
  },
};
</script>

<style  scoped>
  .login{
    width: 400px;
    margin: 0 auto;
  }
  .btn{
    width: 100%;
    font-size: 18px;
  }
</style>